package com.hong.qrpc.codec.constant;

import com.hong.qrpc.codec.Decoder;
import com.hong.qrpc.codec.Encoder;
import com.hong.qrpc.codec.impl.HessianDecoder;
import com.hong.qrpc.codec.impl.HessianEncoder;
import com.hong.qrpc.codec.impl.JSONDecoder;
import com.hong.qrpc.codec.impl.JSONEncoder;

import java.util.HashMap;
import java.util.Map;

/**
 * @Author HQT
 * @Date 2022/8/9 10:46
 */
public abstract class CodecConst {
    public static final int JSON_CODE_TYPE = 1;
    public static final int HESSIAN_CODE_TYPE = 2;

    public static Map<Integer, Decoder> getAllTypeDecoder() {
        Map<Integer, Decoder> map = new HashMap<>();
        map.put(JSON_CODE_TYPE, new JSONDecoder());
        map.put(HESSIAN_CODE_TYPE, new HessianDecoder());
        return map;
    }

    public static Map<Integer, Encoder> getAllTypeEncoder() {
        Map<Integer, Encoder> map = new HashMap<>();
        map.put(JSON_CODE_TYPE, new JSONEncoder());
        map.put(HESSIAN_CODE_TYPE, new HessianEncoder());
        return map;
    }

    public static int getSerializationTypeBy(Encoder encoder) {
        if (encoder instanceof HessianEncoder) {
            return HESSIAN_CODE_TYPE;
        } else if (encoder instanceof JSONEncoder) {
            return JSON_CODE_TYPE;
        } else {
            throw new IllegalArgumentException("unsupported serialization type");
        }
    }
}
